package com.szyjjk.imageCrawler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.szyjjk.entity.SysImageCrawlerFiles;
import com.szyjjk.util.DateUtil;
import com.szyjjk.util.HttpTool;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @Author yangyue
 * @Description //TODO 爬虫下载类
 * @Date 10:42 2019/4/18
 * @Param
 * @return
 **/
public class CrawlerTool {

    private static String textLog;

    private static Logger log = LoggerFactory.getLogger("crawlerLog");

    /**
     * @return
     * @Author yangyue
     * @Description //TODO 访问url 返回包含下载地址的JSON  设置了不要SSL
     * @Date 11:00 2019/4/18
     * @Param
     **/
    public static String doGet(String urls) throws Exception {
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (certificate, authType) -> true).build();
        CloseableHttpClient client = HttpClients.custom()
                .setSslcontext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
        HttpGet httpGet = new HttpGet(urls);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setSocketTimeout(2000)
                .setConnectTimeout(5000).build();
        httpGet.setConfig(requestConfig);
        httpGet.setHeader("X-Requested-With", "XMLHttpRequest");
        HttpResponse response = client.execute(httpGet);
        String resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
        return resultString;
    }




    /*
     * @Author yangyue
     * @Description //TODO 可以添加多个请求头的get请求
     * @Date 15:42 2020/3/11
     * @Param [urls, head]
     * @return java.lang.String
     **/
    public static String doGetOnther(String urls, Map<String, String> head) throws Exception {
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (certificate, authType) -> true).build();
        CloseableHttpClient client = HttpClients.custom()
                .setSslcontext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
        HttpGet httpGet = new HttpGet(urls);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(20000)
                .setSocketTimeout(20000)
                .setConnectTimeout(50000).build();
        httpGet.setConfig(requestConfig);
        if (head != null) {
            for (Object map : head.entrySet()) {
                httpGet.setHeader((String) ((Map.Entry) map).getKey(), (String) ((Map.Entry) map).getValue());
            }
        }
        httpGet.setHeader("accept", "*/*");
        httpGet.setHeader("connection", "Keep-Alive");
        httpGet.setHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        HttpResponse response = client.execute(httpGet);
        String resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
        return resultString;
    }
    //输入StringJSON 返回Lis<String[]> 存储多个数组，每个数组包含文件名、下载链接、文件上传时间、文件大小

    /**
     * @return java.util.List<Map                               <                               String                               ,                               String>> 存储多个数组，每个数组包含文件名、下载链接、文件上传时间、文件大小
     * @Author yangyue
     * @Description //TODO 解析StringJosn
     * @Date 11:06 2019/4/18
     * @Param [jsonString] 包含JSON的String数据
     **/
    public static List<Map<String, String>> jsonStringToList(String jsonString) {
        //将JSON转化为Map
        Map mapString = (Map) JSON.parse(jsonString);
        List<Map<String, String>> jsonList = new ArrayList<>();
        for (Object map : mapString.entrySet()) {
//            System.out.println(((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());
            JSONObject jsonObject = JSON.parseObject(((Map.Entry) map).getValue().toString());
            Map<String, String> jsonMap = new HashMap<String, String>();
            jsonMap.put("name", jsonObject.getString("name"));
            jsonMap.put("startTime", jsonObject.getString("start"));
            jsonMap.put("endTime", jsonObject.getString("end"));
            jsonMap.put("fileSize", jsonObject.getString("size"));
            jsonMap.put("fileURL", jsonObject.getString("fileURL"));
            jsonList.add(jsonMap);
        }
        return jsonList;
    }

    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 当前时间+毫秒
     * @Date 15:20 2019/4/19
     * @Param []
     **/
    public static String oneHourbegin(int mile) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        int mileint = mile;
//        System.out.println(mileint);
        Date afterDate = new Date(now.getTime() + mileint);
//        System.out.println(sdf.format(afterDate ));
        return sdf.format(afterDate);
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 生成随机数
     * @Date 10:13 2019/4/22
     * @Param []
     **/
    public static String getRandomChar(int length) {            //生成随机字符串
        char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(36)]);
        }
        return buffer.toString();
    }


    /**
     * @return void
     * @Author yangyue
     * @Description //TODO 控制台输出  log
     * @Date 11:54 2019/4/22
     * @Param []  message消息  Type 1 MOD  Type 2
     **/
    public static void systemout(String message, String Type) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        Thread.currentThread().setName(Type);
        String ptextLog = "(" + Thread.currentThread().getName() + ")------------" + message + "  时间：" + df.format(new Date());
        //写入日志
        log.info(ptextLog);
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 返回新流程的查询链接
     * @Date 16:13 2019/6/24
     * @Param [satelite]
     **/
    public static String NasaDownUrl(String satelite, String lat_left, String lon_down, String lat_right, String lon_up, Date begTime, Date endTime) {
        //第一版的网址
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dotime = simpleDateFormat.format(begTime);
        String endtime = simpleDateFormat.format(endTime);
//        return "https://ladsweb.modaps.eosdis.nasa.gov/api/v1/files/product=" + satelite + "&collection=61&dateRanges=" + dotime + ".." + endtime + "&areaOfInterest=x" + lat_left + "y" + lon_up + ",x" + lat_right + "y" + lon_down + "&dayCoverage=true&dnboundCoverage=true";


        //第二版的网址
//
        String satelineID = "";
        switch (satelite) {
            case "MOD02QKM":
                satelineID = "C1426416980";
                break;
            case "MOD021KM":
                satelineID = "C1426414410";
                break;
            case "MYD02QKM":
                satelineID = "C1426621826";
                break;
            case "MYD021KM":
                satelineID = "C1426616847";
                break;
            default:
                return "出错！输入不可能参数";
        }
//        String abx= "https://cmr.earthdata.nasa.gov:443/search/granules.json?bounding_box=" + lat_left + "%2C" + lon_down + "%2C" + lat_right + "%2C" + lon_up + "&echo_collection_id=" + satelineID + "-LANCEMODIS&page_num=1&page_size=20&sort_key%5B%5D=-start_date&temporal=" + dotime + "T00%3A00%3A00.000Z%2C" + endTime + "T23%3A59%3A59.000Z";
        return "https://cmr.earthdata.nasa.gov/search/granules.json?bounding_box=" + lat_left + "," + lon_down + "," + lat_right + "," + lon_up + "&echo_collection_id=" + satelineID + "-LANCEMODIS&page_num=1&page_size=100&temporal=" + dotime + "T00:00:00.000Z," + endtime + "T23:59:59.999Z&sort_key=-start_date";
    }

//    public static void main(String[] args) {
//        String reuslt="{\"5139092069\":{\"name\":\"MOD021KM.A2020002.0115.061.2020002192608.hdf\",\"size\":\"179074787\",\"collection\":61,\"ESDT\":\"MOD021KM\",\"start\":\"2020-01-02 01:15:00\",\"end\":\"2020-01-02 01:15:05\",\"FOSDCompression\":null,\"GRingLongitude1\":\"131.479810269846\",\"GRingLongitude2\":\"167.596296255243\",\"GRingLongitude3\":\"157.0069472669\",\"GRingLongitude4\":\"130.575366953762\",\"GRingLatitude1\":\"57.2080147685108\",\"GRingLatitude2\":\"52.0399959653559\",\"GRingLatitude3\":\"35.341961624382\",\"GRingLatitude4\":\"38.9124082057095\",\"status\":\"Online\",\"fileURL\":\"\\/archive\\/allData\\/61\\/MOD021KM\\/2020\\/002\\/MOD021KM.A2020002.0115.061.2020002192608.hdf\"},\"5139092070\":{\"name\":\"MOD021KM.A2020002.0120.061.2020002192708.hdf\",\"size\":\"177284265\",\"collection\":61,\"ESDT\":\"MOD021KM\",\"start\":\"2020-01-02 01:20:00\",\"end\":\"2020-01-02 01:20:05\",\"FOSDCompression\":null,\"GRingLongitude1\":\"130.35401268255\",\"GRingLongitude2\":\"156.587655941508\",\"GRingLongitude3\":\"150.337181716787\",\"GRingLongitude4\":\"128.132449026764\",\"GRingLatitude1\":\"39.3210560670442\",\"GRingLatitude2\":\"35.5261016902383\",\"GRingLatitude3\":\"18.0336550735987\",\"GRingLatitude4\":\"21.080969388194\",\"status\":\"Online\",\"fileURL\":\"\\/archive\\/allData\\/61\\/MOD021KM\\/2020\\/002\\/MOD021KM.A2020002.0120.061.2020002192708.hdf\"},\"5139090984\":{\"name\":\"MOD021KM.A2020002.0255.061.2020002192554.hdf\",\"size\":\"158629179\",\"collection\":61,\"ESDT\":\"MOD021KM\",\"start\":\"2020-01-02 02:55:00\",\"end\":\"2020-01-02 02:55:05\",\"FOSDCompression\":null,\"GRingLongitude1\":\"106.722372139871\",\"GRingLongitude2\":\"139.762148556842\",\"GRingLongitude3\":\"130.530594341828\",\"GRingLongitude4\":\"105.416444162768\",\"GRingLatitude1\":\"53.2147517676979\",\"GRingLatitude2\":\"48.4690640153067\",\"GRingLatitude3\":\"31.5216231662283\",\"GRingLatitude4\":\"34.919359626766\",\"status\":\"Online\",\"fileURL\":\"\\/archive\\/allData\\/61\\/MOD021KM\\/2020\\/002\\/MOD021KM.A2020002.0255.061.2020002192554.hdf\"},\"5139095396\":{\"name\":\"MOD021KM.A2020002.0300.061.2020002192728.hdf\",\"size\":\"156995270\",\"collection\":61,\"ESDT\":\"MOD021KM\",\"start\":\"2020-01-02 03:00:00\",\"end\":\"2020-01-02 03:00:05\",\"FOSDCompression\":null,\"GRingLongitude1\":\"105.190903912706\",\"GRingLongitude2\":\"130.150986193032\",\"GRingLongitude3\":\"124.447568395868\",\"GRingLongitude4\":\"102.75934896697\",\"GRingLatitude1\":\"35.3058789640065\",\"GRingLatitude2\":\"31.6899642647203\",\"GRingLatitude3\":\"14.0991670724183\",\"GRingLatitude4\":\"17.1009649440412\",\"status\":\"Online\",\"fileURL\":\"\\/archive\\/allData\\/61\\/MOD021KM\\/2020\\/002\\/MOD021KM.A2020002.0300.061.2020002192728.hdf\"},\"5139091862\":{\"name\":\"MOD021KM.A2020002.0430.061.2020002192656.hdf\",\"size\":\"167564944\",\"collection\":61,\"ESDT\":\"MOD021KM\",\"start\":\"2020-01-02 04:30:00\",\"end\":\"2020-01-02 04:30:05\",\"FOSDCompression\":null,\"GRingLongitude1\":\"81.4089004756423\",\"GRingLongitude2\":\"128.6800433241\",\"GRingLongitude3\":\"112.873083447885\",\"GRingLongitude4\":\"82.0905424050547\",\"GRingLatitude1\":\"67.1294475098738\",\"GRingLatitude2\":\"60.3895747173664\",\"GRingLatitude3\":\"44.5880150326518\",\"GRingLatitude4\":\"48.7563584914702\",\"status\":\"Online\",\"fileURL\":\"\\/archive\\/allData\\/61\\/MOD021KM\\/2020\\/002\\/MOD021KM.A2020002.0430.061.2020002192656.hdf\"}}";
//        List<String> ar=NasaJsonStringToList(reuslt);
//    }

    /**
     * @return java.util.List<java.util.Map                               <                               java.lang.String                               ,                               java.lang.String>>
     * @Author yangyue
     * @Description //TODO 处理JosnString  NASA
     * @Date 16:52 2019/6/20
     * @Param [jsonString]
     **/
    public static List<SysImageCrawlerFiles> NasaJsonStringToList(String jsonString) {
        List<SysImageCrawlerFiles> resultList = new ArrayList<>();
        //将JSON转化为Map
        if (jsonString.length() < 8) {
            return null;
        }
        //这个是第二版的解析
        Map mapString = (Map) JSON.parse(jsonString);
        for (Object map : mapString.entrySet()) {
//            System.out.println(((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());
            JSONObject jsonObject = JSON.parseObject(((Map.Entry) map).getValue().toString());
            JSONArray jsonEntry = jsonObject.getJSONArray("entry");
            for (int i = 0; i < jsonEntry.size(); i++) {
                JSONObject partDaily = jsonEntry.getJSONObject(i);
                JSONArray jsonEntrylinks = partDaily.getJSONArray("links");
                JSONObject partDailylinks = jsonEntrylinks.getJSONObject(0);
                SysImageCrawlerFiles sysImageCrawlerFiles = new SysImageCrawlerFiles();
                sysImageCrawlerFiles.setFileDownloadUrl(partDailylinks.getString("href"));//URL
                sysImageCrawlerFiles.setFileName(String.valueOf(partDaily.getString("producer_granule_id")));    //name
                // 原数据为 2020-06-04T04:55:25.375Z  需要进行截取
                sysImageCrawlerFiles.setFileUploadTime(DateUtil.stringToDate2(String.valueOf(partDaily.getString("updated")).replaceAll("T", " ").substring(0, 19)));
                String longLat=partDaily.getJSONArray("polygons").get(0).toString();
                sysImageCrawlerFiles.setFileLonglat(longLat.substring(2,longLat.length()-2));
                resultList.add(sysImageCrawlerFiles);
            }
        }
        return resultList;

//        if (jsonString.length() < 8) {
//            return null;
//        }
//
//        Map mapString = (Map) JSON.parse(jsonString);
//        for (Object map : mapString.entrySet()) {
//            SysImageCrawlerFiles sysImageCrawlerFiles=new SysImageCrawlerFiles();
//            JSONObject jsonObject = JSON.parseObject(((Map.Entry) map).getValue().toString());
//            String GRingLatitude1=  jsonObject.getString("GRingLatitude1");
//            String GRingLatitude2=  jsonObject.getString("GRingLatitude2");
//            String GRingLatitude3=  jsonObject.getString("GRingLatitude3");
//            String GRingLatitude4=  jsonObject.getString("GRingLatitude4");
//            String GRingLongitude1=  jsonObject.getString("GRingLongitude1");
//            String GRingLongitude2=  jsonObject.getString("GRingLongitude2");
//            String GRingLongitude3=  jsonObject.getString("GRingLongitude3");
//            String GRingLongitude4=  jsonObject.getString("GRingLongitude4");
//            sysImageCrawlerFiles.setFileLonglat(GRingLongitude1+","+GRingLatitude1+";"+GRingLongitude2+","+GRingLatitude2+";"+GRingLongitude3+","+GRingLatitude3+";"+GRingLongitude4+","+GRingLatitude4);
//            String  time=jsonObject.getString("start");
//            try {
//                sysImageCrawlerFiles.setFileUploadTime(HttpTool.longSimpDate.parse(time));
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//            sysImageCrawlerFiles.setFileSize(jsonObject.getString("size"));
//            sysImageCrawlerFiles.setFileName(jsonObject.getString("name"));
//            sysImageCrawlerFiles.setFileDownloadUrl("https://ladsweb.modaps.eosdis.nasa.gov" + jsonObject.getString("fileURL"));
//            resultList.add(sysImageCrawlerFiles);
//        }
//
//        return resultList;
    }


    /**
     * @return boolean
     * @Author yangyue
     * @Description //TODO Wget下载
     * @Date 19:07 2019/6/20
     * @Param [downUrl]
     **/
    public static SysImageCrawlerFiles wgetDown(String downUrl, String savePath, String fileNmae, String wegetPath, String name, String pwd, String soutType) {
        SysImageCrawlerFiles sysImageCrawlerFiles=new SysImageCrawlerFiles();
//        systemout("正在下载：" + fileNmae, soutType);
        long filesize = 0;
        long begin = System.currentTimeMillis();
        try {
            String filenameHead = fileNmae+".Temp";
            String downulr = "cmd /c start  " + wegetPath + "  ftp://" + downUrl + "  --ftp-user=" + name + " --ftp-password=" + pwd + "  -O " + savePath + filenameHead;
            Process process = Runtime.getRuntime().exec(downulr);
            if (process != null) {
                process.getOutputStream().close();
            }
            InputStream in = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                // do nothing...啥也不干
            }
            Thread.sleep(500);
            //下载完成
            File file = new File(savePath + filenameHead);
            if (file.exists()) {
                File newfile = new File(file.getParent() + File.separator + fileNmae);//创建新名字的抽象文件
                if (file.renameTo(newfile)) {
                    systemout(fileNmae + "，重命名成功", soutType);
                    filesize = newfile.length();
                } else {
                    systemout(fileNmae + "，重命名失败！新文件名已存在", soutType);
                }
            } else {
                systemout(fileNmae + "，重命名文件不存在", soutType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            systemout(fileNmae + "下次出错    " + e, soutType);
            return  null;
        }
        long mills = System.currentTimeMillis() - begin;
        systemout(fileNmae + "下载完成，耗时"+CrawlerTool.formatDuring(mills), soutType);
        sysImageCrawlerFiles.setFileName(fileNmae); //名字
        sysImageCrawlerFiles.setFileType(soutType); //类别
        sysImageCrawlerFiles.setFileSize(filesize+"");  //大小
        sysImageCrawlerFiles.setFileDownloadUrl(downUrl);//下载链接

        return sysImageCrawlerFiles;
    }




    /**
     * @Author yangyue
     * @Description //TODO wget 下载通用
     * @Date 10:26 2020/5/20
     * @Param [wgetCode 下载命令,  downLoadPath 存储地址, fileName  文件名]
     * @return long
     **/
    public static long wgetDownLoad(String wgetCode, String downLoadPath, String fileName){
        long filesize = 0; 
        try {
            wgetCode=wgetCode+".temp";
             Process process = Runtime.getRuntime().exec(wgetCode);
            if (process != null) {
                process.getOutputStream().close();
            }
            InputStream in = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                // do nothing...啥也不干
            }
            Thread.sleep(500);
            //下载完成
            File file = new File(downLoadPath + "//"+fileName+".temp");
            if (file.exists()) {
                File newfile = new File(file.getParent() + File.separator + fileName);//创建新名字的抽象文件
                if (file.renameTo(newfile)) {
                    filesize = newfile.length();
                } else {
                    System.out.println(fileName + "，重命名失败！新文件名已存在");
                    file.delete();
                    return 0;
                }
            } else {
                System.out.println(fileName + "，重命名文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  0;
        } 
        return  filesize;
    }




    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO
     * @Date 16:21 2019/12/9
     * @Param [downUrl 下载链接, savePath 存储地址, fileNmae 文件名 , wegetPath, name, pwd, soutType]
     **/
//    public static SysImageCrawlerFiles wgetDownH2B(String downUrl, String savePath, String fileNmae, String wegetPath, String name, String pwd, String soutType) {
//        //确认下载的文件夹有没有存在
//        File fileSavePath = new File(savePath);
//        if (!fileSavePath.exists() && !fileSavePath.isDirectory()) {
//            fileSavePath.mkdir();
//        }
//        long filesize = 0;
//        long begin = System.currentTimeMillis();
//        try {
//            //找到最后一个/ 然后截取
//            String TempfileNmae = fileNmae + ".temp"; //临时文件
//
//            String downulr = "cmd /c start  " + wegetPath + "  ftp://" + downUrl + "  --ftp-user=" + name + " --ftp-password=" + pwd + "  -O " + savePath + "\\" + TempfileNmae;
//            Process process = Runtime.getRuntime().exec(downulr);
//            if (process != null) {
//                process.getOutputStream().close();
//            }
//            InputStream in = process.getInputStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(in));
//            String tmp = null;
//            while ((tmp = br.readLine()) != null) {
//                // do nothing...啥也不干
//            }
//            Thread.sleep(500);
//            //下载完成
//            File file  = new File(savePath + TempfileNmae);
//            if (file.exists()) {
//                File newfile = new File(file.getParent() + File.separator + fileNmae);//创建新名字的抽象文件
//                if (file.renameTo(newfile)) {
//                    systemout(fileNmae + "，重命名成功", soutType);
//                    filesize = newfile.length();
//                } else {
//                    systemout(fileNmae + "重命名失败！新文件名已存在", soutType);
//                }
//            } else {
//                systemout(fileNmae + "，重命名文件不存在", soutType);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//
//
//
//        long mills = System.currentTimeMillis() - begin;
//        return mills + "," + filesize;
//    }


    /**
     * @return int
     * @Author yangyue
     * @Description //TODO 获取今天是当年的第几天
     * @Date 9:34 2019/6/24
     * @Param []
     **/
    public static int getYeayDay() {
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int i = ca.get(Calendar.DAY_OF_YEAR);
        return i;
    }


    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 毫秒转分钟
     * @Date 10:49 2019/5/17
     * @Param [time]
     **/
    public static String formatDuring(long mss) {

        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return hours + " 小时 " + minutes + " 分钟 "
                + seconds + " 秒 ";
    }


    /**
     * @return void
     * @Author yangyue
     * @Description //TODO 定时器，定时执行下一步操作
     * @Date 15:42 2019/6/26
     * @Param [time, type] time   type=0 是今天  type=1 是明天
     **/
    public static void showDayTime(Date time, int type) {
        Calendar setCalendar = Calendar.getInstance();
        setCalendar.setTime(time);
        setCalendar.get(Calendar.HOUR_OF_DAY);
        setCalendar.get(Calendar.MINUTE);

        Calendar calendar = Calendar.getInstance();
        if (type == 1) {
            calendar.add(calendar.DATE, 1);
        }
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        int hour = setCalendar.get(Calendar.HOUR_OF_DAY);
        int min = setCalendar.get(Calendar.MINUTE);
        int mil = setCalendar.get(Calendar.SECOND);
//        CrawlerTool.systemout("下载任务结束，下次任务开始时间" + year + "-" + (month + 1) + "-" + day + "  " + hour + ":" + min + ":" + mil, 1);
        calendar.set(year, month, day, hour, min, mil);//设置要执行的日期时间
        Date defaultdate = calendar.getTime();
        Timer dTimer = new Timer();
        dTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
//                    crawlerDo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, defaultdate, 24 * 60 * 60 * 1000);//24* 60* 60 * 1000
    }


    // index:0,value: 10:00;
    // index:1,value: 13:00;
    // index:2,value: 16:00;
    // index:3,value: 17:00;
    // index:4,value: 20:00;
    // index:5,value: 23:30;

    /**
     * @return java.util.List<java.util.Date>
     * @Author yangyue
     * @Description //TODO 获取节点数据
     * @Date 16:54 2019/7/17
     * @Param []
     **/
    public static List<Long> getNodeDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<Long> NodeDate = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        //十点
        Date resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());
        //下午一点
        calendar.set(Calendar.HOUR_OF_DAY, 13);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());
        //四点
        calendar.set(Calendar.HOUR_OF_DAY, 16);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());
        //五点
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());
        //八点
        calendar.set(Calendar.HOUR_OF_DAY, 20);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());
        //十一点班
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 30);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());

        calendar.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        resultDate = calendar.getTime();
        NodeDate.add(resultDate.getTime());

        return NodeDate;
    }


    /**
     * @return boolean
     * @Author yangyue
     * @Description //TODO 判断时间是不是在当天24小时内
     * @Date 16:03 2019/7/22
     * @Param [inputJudgeDate]
     **/
    public static boolean isToday(Date inputJudgeDate) {
        boolean flag = false;
        //获取当前系统时间
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(nowDate);
        String subDate = format.substring(0, 10);
        //定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";
        Date paseBeginTime = null;
        Date paseEndTime = null;
        try {
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);

        } catch (ParseException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        if (inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
            flag = true;
        }
        return flag;
    }


    /**
     * 获取指定时间对应的毫秒数
     *
     * @param time "HH:mm:ss"
     * @return
     */
    public static long getTimeMillis(String time) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
            Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
            return curDate.getTime();
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @return java.lang.String
     * @Author yangyue
     * @Description //TODO 返回wgetpath
     * @Date 11:18 2019/8/18
     * @Param []
     **/
    public static String backWgetPath(String key) {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = CrawlerTool.class.getClassLoader().getResourceAsStream("application.properties");
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e) {

            e.printStackTrace();
        }
        //获取key对应的value值
        String time_interval = properties.getProperty(key);
        return time_interval;
    }


    /**
     * @return java.util.Date
     * @Author yangyue
     * @Description //TODO 输入时间和增加的天数，转间化为当前日期的时或者增加后的天数
     * @Date 10:28 2019/8/27
     * @Param [needDate, number]
     **/
    public static Date getTimeTaday(Date needDate, int number) {
        //之前的时间
        Calendar needCa = Calendar.getInstance();
        needCa.setTime(needDate);
        //之后的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, number);
        calendar.set(Calendar.HOUR_OF_DAY, needCa.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, needCa.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, needCa.get(Calendar.SECOND));
        return calendar.getTime();
    }


    /**
     * @return java.util.Date
     * @Author yangyue
     * @Description //TODO 处理输入事件
     * @Date 11:28 2019/12/9
     * @Param [needDate, year, month, day, hour, mi]
     **/
    public static Date setTime(Date needDate, int year, int month, int day, int hour, int mi) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(needDate);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + year);//年
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month);  //月
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);//日
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour); //时
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + mi);//分
        return calendar.getTime();
    }


//    public static void wegetNoThread(String dwonUrl, String savePath, String wgetPath, String crawlerID, String downType) {
//        dwonUrl = dwonUrl.replaceAll("https://nrt3.modaps.eosdis.nasa.gov/archive/allData/", "https://nrt3.modaps.eosdis.nasa.gov/api/v2/content/archives/allData/");
//        String fileNmae = CrawlerTool.getPathHtmlName(dwonUrl);
//        CrawlerTool.systemout("需要下载的为文件为：" + fileNmae, "Modis绿潮");
////        String DownLoadresult = CrawlerTool.wgetDown(dwonUrl, WholeSysCrawlerInfo.getSavePath(), fileNmae, WEGETPATH);
//        long begin = System.currentTimeMillis();
//        long fileLength = 0;
//        try {
//            String filenameHead = fileNmae.replaceAll(".hdf", "") + "_Temp.hdf";
//            String downulr = "cmd /c start  " + wgetPath + "  -e robots=off -m -nd -R .html,.tmp -nH --cut-dirs=4 \"DOWNURL\" --header \"Authorization: Bearer 6B9F7954-90F1-11E9-9988-815845326583\" -O " + savePath + "/" + filenameHead;
//            downulr = downulr.replaceAll("DOWNURL", dwonUrl);
//            Process process = Runtime.getRuntime().exec(downulr);
//            if (process != null) {
//                process.getOutputStream().close();
//            }
//            InputStream in = process.getInputStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(in));
//            String tmp = null;
//            while ((tmp = br.readLine()) != null) {
//                // do nothing...啥也不干
//            }
//            Thread.sleep(500);
//            //下载完成
////            System.out.println(savePath + filenameHead+"   真正的名字:   "+fileNmae);
//            File file = new File(savePath + filenameHead);
//            if (file.exists()) {
//                File newfile = new File(file.getParent() + File.separator + fileNmae);//创建新名字的抽象文件
//                if (file.renameTo(newfile)) {
//                    CrawlerTool.systemout("重命名成功：" + fileNmae, "Modis绿潮");
//                    fileLength = newfile.length();
//                } else {
//                    CrawlerTool.systemout("重命名失败！新文件名已存在,文件删除，文件名" + fileNmae, "补充下载 Modis绿潮");
//                    file.delete();
//                }
//            } else {
//                CrawlerTool.systemout("重命名文件不存在啊" + fileNmae, "Modis绿潮");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        long mills = System.currentTimeMillis() - begin;
//        CrawlerTool.systemout(fileNmae + "文件 补充下载完毕 ，下载耗时" + CrawlerTool.formatDuring(mills) + "s", "Modis绿潮");
//        if (mills < 1000) {
//            CrawlerTool.systemout(fileNmae + "下载存在问题，耗时" + CrawlerTool.formatDuring(mills), "Modis绿潮");
//        } else {
//            //存库
////            CrawlerFlow.saveDataMod(fileNmae, dwonUrl, crawlerID, downType, fileLength + "");
//        }
//    }
    /**
     * @Author yangyue
     * @Description //TODO 传入HTML路径，输出文件名
     * @Date 16:03 2019/11/4
     * @Param [path, name]
     * @return java.lang.String
     **/
    public static String getPathHtmlName(String path) {
        String dopathc[]=path.split("/");
        return dopathc[dopathc.length-1];
    }
}
